package com.codingstudy.login.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingstudy.login.dao.SysFrontendMenuTableDao;
import com.codingstudy.login.entity.SysFrontendMenuTable;
import com.codingstudy.login.service.SysFrontendMenuTableService;
import com.codingstudy.login.vo.Menuvo;
import com.codingstudy.login.vo.SysFrontendMenuTablevo;
import com.codingstudy.login.vo.SysFrontendVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (SysFrontendMenuTable)表服务实现类
 * 该类由EasyCode工具生成
 * @author 小明哥
 * @since 2020-03-07 13:42:36
 */
@Service("sysFrontendMenuTableService")
public class SysFrontendMenuTableServiceImpl extends ServiceImpl<SysFrontendMenuTableDao, SysFrontendMenuTable> implements SysFrontendMenuTableService {

    /**
     * 查所有的前端菜单（编辑使用）
     */
    @Override
   public List<SysFrontendVo> getAllMenuList(){
       return this.baseMapper.getAllMenuList();
    }

    /**
     * 根据登录账号，获得前端展现的菜单
     * 控制前端菜单的权限
     * @param username
     * @return
     */
    @Override
    public List<SysFrontendMenuTablevo> getMenusByUserName(String username){
        //查出来所有的菜单
//        List<SysFrontendMenuTable> menuList = this.baseMapper.getMenusByUserName(username);
        List<SysFrontendMenuTablevo> sysFrontendMenuTablevos = queryMenuList();
        return sysFrontendMenuTablevos;

    }
    /**
     * 对象实体类转换
     * @param rcMenu
     * @return
     */
    private SysFrontendMenuTablevo rcMenuTORcMenuVO(SysFrontendMenuTable rcMenu) {
        SysFrontendMenuTablevo rcMenuVO = new SysFrontendMenuTablevo();
        BeanUtils.copyProperties(rcMenu, rcMenuVO);
        //前端需要的数据
        Menuvo menuvo = new Menuvo();
        menuvo.setIcon(rcMenu.getIocn());
        menuvo.setTitle(rcMenu.getTitle());
        rcMenuVO.setMeta(menuvo);

        return rcMenuVO;
    }


    /**
     * 根据一级递归调用子级
     *	reversed方法 表示数字越大靠前
     * @param menuList
     * @param rootMenuList
     * @return
     */
    private void findSubCategory(List<SysFrontendMenuTable> menuList, List<SysFrontendMenuTablevo> rootMenuList) {

        // 遍历一级
        for (SysFrontendMenuTablevo rcMenuVO : rootMenuList) {

            List<SysFrontendMenuTablevo> rcMenuVOList = new ArrayList<>();
            // 查找子级
            for (SysFrontendMenuTable rcMenu : menuList) {
                // 判断当前目录是否是子父级关系
                if (rcMenuVO.getFrontendMenuId().equals(rcMenu.getPid())) {

                    rcMenuVOList.add(rcMenuTORcMenuVO(rcMenu));
                }
                // 递归调用，不管有几级菜单，都能够适用

                findSubCategory(menuList, rcMenuVOList);

                // 类目显示排序，
                rcMenuVOList.sort(Comparator.comparing(SysFrontendMenuTablevo::getFrontendMenuSort));
            }
            // 最后把查到的子级保存到一级目录中
            rcMenuVO.setChildren(rcMenuVOList);
        }
    }

    /**
     * 	各级菜单列表展示
     * @param menu
     * @param
     * @return
     */
    public List<SysFrontendMenuTablevo> queryMenuList() {
        List<SysFrontendMenuTable> menuList = null;
//        Page<RcMenu> page = PageHelper.startPage(pageObject.getPageNum(), pageObject.getPageSize());
//        // 将用户页面提交的参数拷贝到查询条件去
//        SpringBeanUtils.copyPropertiesIgnoreNull(pageObject, page);
        //先查出全部菜单


        menuList = this.baseMapper.selectAll();

        //获取一级菜单	0代表一级菜单 .reversed()
        List<SysFrontendMenuTablevo> rootMenuList = menuList.stream()
                .filter(e -> e.getPid().equals("0"))
                .map(this::rcMenuTORcMenuVO)
                .sorted(Comparator.comparing(SysFrontendMenuTablevo::getFrontendMenuSort))
                .collect(Collectors.toList());
        //查找字节点
        findSubCategory(menuList,rootMenuList);

        // 将真实分页信息拷贝到用户页面
//        SpringBeanUtils.copyPropertiesIgnoreNull(page, pageObject);
        //返回值
        System.out.println(rootMenuList);
        return rootMenuList;
    }








    /**
     * 根据一级递归调用子级
     *	reversed方法 表示数字越大靠前
     * @param menuList
     * @param rootMenuList
     * @return
     */


}